#! /bin/env python3
# coding: utf-8

import pymysql
def run_sql(db_info, sql, values=None, res_type=None):
    if isinstance(db_info, dict):
        try:
            if 'db_port' in db_info.keys():
                connect_db = pymysql.connect(db_info['db_host'], db_info['db_user'], db_info['db_pass'], db_info['db_name'], db_info['db_port'])
                #connect_db = pymysql.connect(db_info['db_host'], db_info['db_user'], db_info['db_pass'],db_info['db_name'], db_info['db_port'],cursorclass = pymysql.cursors.DictCursor)

            else:
                connect_db = pymysql.connect(db_info['db_host'], db_info['db_user'], db_info['db_pass'], db_info['db_name'])
            if res_type == "dict":
                cursor = connect_db.cursor(pymysql.cursors.DictCursor)
            else:
                cursor = connect_db.cursor()
            if not values:
                cursor.execute(sql)
            else:
                cursor.executemany(sql,values)
            result = cursor.fetchall()
            connect_db.commit()
            cursor.close()
            return result
        except Exception as e:
            raise e
